package com.tanker.controller;

import org.bouncycastle.util.encoders.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tanker.common.ErrorCode;
import com.tanker.common.ResponseObject;
import com.tanker.domain.TankerReq;
import com.tanker.service.BuyCpicService;

/**
 * @author xqxue
 * @time 2018年1月17日 上午10:24:06
 */
@RestController
@RequestMapping("/tanker/")
public class CpicController {
	private static final Logger logger = LoggerFactory.getLogger(CpicController.class);
	
	@Autowired
	private BuyCpicService buyCpicService;
	
	@RequestMapping(value="cpic", method = RequestMethod.POST)
    public ResponseObject<?> cpic(@RequestBody TankerReq req){
		if(req==null || StringUtils.isEmpty(req.getXml())) {
			logger.info("tanker请求买保险接口xml is null");
			return new ResponseObject<String>(ErrorCode.FAILED);
		}
		
		try {
			String xml = req.getXml();
			xml = new String(Base64.decode(xml.getBytes()));
			String result = buyCpicService.sendPost(xml);
			logger.info("tanker请求买保险接口成功result={}", result);
			return new ResponseObject<String>(ErrorCode.SUCCESS, new String(Base64.encode(result.getBytes())));
		} catch (Exception e) {
			logger.error("tanker请求买保险接口异常", e);
			return new ResponseObject<String>(ErrorCode.FAILED, "请求买保险接口异常");
		}
    }
	
	@RequestMapping(value="test", method = RequestMethod.POST)
    public ResponseObject<?> test(@RequestBody TankerReq req){
			logger.info("请求接口666");
			return new ResponseObject<String>(ErrorCode.SUCCESS, "66666");
    }
}
